module.exports = function($scope, $state, $stateParams, $modalInstance, item,$resource,$modal,FileUploader,toaster){  
    console.log(item);
    if(item != undefined){
        var id = item.id;
        console.log(id);
    }
    
    if(id){
        $resource('/api/as/gc/news/getContentsInfo', {}, {}).
        save({'id' : id},function(res) {
            if (res.errcode !== 0) {
                toaster.error({title: "", body:res.errmsg});
                return;
            }
            console.log(res);
            $scope.info = res.data;
        });
    }
    var uploader = $scope.uploader = new FileUploader({
        url: 'http://cl.juyouhx.com/oss.php/oss/webuploader1?topdir=aa&selfdir=bb'
    });

    uploader.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    
    
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        $scope.info.header_photo = response.savename;
    };

    // 副图
    var uploader1 = $scope.uploader1 = new FileUploader({
        url: 'http://cl.juyouhx.com/oss.php/oss/webuploader1?topdir=aa&selfdir=bb'
    });

    uploader1.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    
    
    uploader1.onSuccessItem = function(fileItem, response, status, headers) {
        $scope.info.drawing_photo = response.savename;
    };
	
    $scope.save = function () {
            if (id) {
                var para = {
                    'id':id
                }    
                if ($scope.info.title!=''&& $scope.info.content!=''&& $scope.info.url!='') {
                    para = angular.extend($scope.info,para);
                    console.log(para);
                    $resource('/api/as/gc/news/updateNews', {}, {}).
                    save(para,function(res) {
                        if (res.errcode !== 0) {
                            toaster.error({title: "", body:res.errmsg});
                            return;
                        }
                        toaster.success({title: "", body:"修改成功!"});
                        $modalInstance.close();
                        // $state.go('app.headline');
                    });
                } else {
                    toaster.error({title: "", body:"请将数据补充完整!"});
                }
                
            } else {
                if ($scope.info.title!=''&&$scope.info.content!=''&&$scope.info.url!='') {
                    console.log(para);
                    $resource('/api/as/gc/news/saveinfo', {}, {}).
                    save($scope.info,function (res) {
                        if (res.errcode !== 0) {
                            toaster.error({title: "", body:res.errmsg});
                            return;
                        }
                        console.log(res);
                        toaster.success({title: "", body:"添加成功!"});
                        $modalInstance.close();   
                    });
                } else {
                    toaster.error({title: "", body:"请将数据补充完整!"});
                }
                
            }

        
        
        
    };

};